package com.zpark.drug.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.common.ResultMapUtil;
import com.zpark.drug.pojo.Loss;
import com.zpark.drug.service.LossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/loss")
public class LossController {
    @Autowired
    private LossService lossService;

    @RequestMapping
    public String login(){
        return "/loss";
    }
    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/lossPage")
    public String lossPage(){
        return "/lossPage";
    }
    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping(value = "/lossQueryById")
    public Object lossQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Loss loss = lossService.findById(id);
        model.addAttribute("obj",loss);
        return "/lossPage";
    }
    /**
     * 获取所有供应商
     * @return
     */
    @RequestMapping(value = "/lossList")
    @ResponseBody
    public Object lossList(){
        List<Loss> lossList = lossService.findAll();
        return ResultMapUtil.getHashMapList(lossList);
    }

    /**
     * 添加一个供应商
     * @return
     */
    @RequestMapping(value = "/lossAdd")
    @ResponseBody
    public Object lossAdd(Loss loss){
        try {
            int i = lossService.addLoss(loss);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/lossEdit")
    @ResponseBody
    public Object lossEdit(Loss loss){
        try {
            int i = lossService.updateLoss(loss);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * @return
     */
    @RequestMapping(value = "/lossDelById")
    @ResponseBody
    public Object lossDelById(Integer id){
        try {
            int i = lossService.deleteLossById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 分页查询供应商列表
     * @return
     */
    @RequestMapping(value = "/lossQueryPage")
    @ResponseBody
    public Map<String, Object> lossQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Loss> lossPageInfo = lossService.findPage(page, limit, param);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", lossPageInfo.getTotal());
        map.put("data", lossPageInfo.getList());
        return map;
    }

}
